package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.MarketRole;
import com.cskaoyan.bean.zhangshizhi.web.role.*;
import com.cskaoyan.config.zhangshizhi.CountTime;
import com.cskaoyan.service.RoleService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 角色管理
 *
 * @author little ants（张时智）
 * @since 2022/06/25 17:06
 */

@RestController
@RequestMapping("admin/role")
public class RoleController {

    @Autowired
    RoleService roleService;

    /**
     * 获取全部角色信息（查询）
     *
     * @param roleBO
     * @return com.cskaoyan.bean.BaseRespVo
     * @author little ants
     * @since 2022/06/30 17:43
     */
    @CountTime
    @RequiresPermissions(value = {"*", "admin:role:list"}, logical = Logical.OR)
    @RequestMapping("list")
    public BaseRespVo roleSelect(RoleListBO roleBO) {
        RoleListVO roleList = roleService.getRoleList(roleBO);
        return new BaseRespVo().ok(roleList);

    }


    /**
     * 增加角色（添加）
     *
     * @param role
     * @return com.cskaoyan.bean.BaseRespVo
     * @author little ants
     * @since 2022/06/30 17:43
     */
    @CountTime
    @RequiresPermissions(value = {"*", "admin:role:create"}, logical = Logical.OR)
    @RequestMapping("create")
    public BaseRespVo roleCreate(@RequestBody MarketRole role) {
        MarketRole roleInfo = roleService.addRole(role);
        return new BaseRespVo().ok(roleInfo);

    }

    /**
     * 删除角色（删除）
     *
     * @param role
     * @return com.cskaoyan.bean.BaseRespVo
     * @author little ants
     * @since 2022/06/30 17:43
     */
    @CountTime
    @RequiresPermissions(value = {"*", "admin:role:delete"}, logical = Logical.OR)
    @RequestMapping("delete")
    public BaseRespVo roleRemove(@RequestBody MarketRole role) {
        roleService.deleteRole(role);
        return new BaseRespVo().ok();

    }

    /**
     * 更新角色信息(编辑)
     *
     * @param role
     * @return com.cskaoyan.bean.BaseRespVo
     * @author little ants
     * @since 2022/06/30 17:44
     */
    @CountTime
    @RequiresPermissions(value = {"*", "admin:role:update"}, logical = Logical.OR)
    @RequestMapping("update")
    public BaseRespVo roleUpdate(@RequestBody MarketRole role) {
        roleService.updateRole(role);
        return new BaseRespVo().ok();

    }

    /**
     * 获取角色权限（授权）
     *
     * @param roleId
     * @return com.cskaoyan.bean.BaseRespVo
     * @author little ants
     * @since 2022/06/30 17:44
     */
    @CountTime
    @RequiresPermissions(value = {"*", "admin:role:permissions"}, logical = Logical.OR)
    @GetMapping("permissions")
    public BaseRespVo rolePermissions(Integer roleId) {
        PermissionsVO permissionsVO = roleService.selectRolePermissions(roleId);
        return new BaseRespVo().ok(permissionsVO);

    }

    /**
     * 修改角色权限（授权）
     *
     * @param permissionsBO
     * @return com.cskaoyan.bean.BaseRespVo
     * @author little ants
     * @since 2022/06/30 17:44
     */
    @CountTime
    @RequiresPermissions(value = {"*", "admin:role:permissions"}, logical = Logical.OR)
    @PostMapping("permissions")
    public BaseRespVo rolePermissionsUpdate(@RequestBody RolePermissionsBO permissionsBO) {
        roleService.updateRolePermissions(permissionsBO);
        return new BaseRespVo().ok();

    }


    /**
     * 获取用户角色
     *
     * @return com.cskaoyan.bean.BaseRespVo
     * @author little ants
     * @since 2022/06/30 17:45
     */

    @RequiresPermissions(value = {"*", "admin:role:list"}, logical = Logical.OR)
    @RequestMapping("options")
    public BaseRespVo roeOfAdmin() {
        RoleSimpleListVO roleSimpleListVO = roleService.getAllRoleSimpleInfo();
        return new BaseRespVo().ok(roleSimpleListVO);

    }


}
